//
//  ClubAddressEmptyView.swift
//  77Club
//
//  Created by Apple on 2024/9/20.
//

import UIKit

class ClubAddressEmptyView: UIView {

    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_address_empty").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold)
        .alignment(.center).text("还没有地址哦，请添加地址~").end()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            iconImageView
            titleLabel
        }
        
        iconImageView.top(0).width(96.0.Scale).height(131.0.Scale).centerHorizontally()
        titleLabel.leading(20).trailing(20)
        titleLabel.Top == iconImageView.Bottom + 24.0.Scale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class ClubAddressCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12).end()
    private let addressLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold).line(0).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_medium).end()
    private let phoneLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(12.font_medium).end()
    private let lineView = c_make_ex(UIView.self)
        .bgColor("#F2F3F5").end()
    private let editButton = c_make_ex(UIButton.self)
        .text("修改").color("#7B7D80").font(12.font_medium).end()
    private let copyButton = c_make_ex(UIButton.self)
        .text("复制").color("#7B7D80").font(12.font_medium).end()
    private let delButton = c_make_ex(UIButton.self)
        .text("删除").color("#7B7D80").font(12.font_medium).end()
    
    var model: ClubMineAddressModel?{
        didSet{
            guard let md = model else { return }
            
            nameLabel.text = md.consignee
            phoneLabel.text = md.mobile
            addressLabel.text = md.uaddress
        }
    }
    
    var editBlock: ((_ model: ClubMineAddressModel) -> Void)?
    var delBlock: ((_ model: ClubMineAddressModel) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                addressLabel
                nameLabel
                phoneLabel
                lineView
                editButton
                copyButton
                delButton
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(12.0.Scale).bottom(0)
        addressLabel.leading(12.0.Scale).trailing(12.0.Scale).top(12.0.Scale)
        nameLabel.leading(12.0.Scale)
        nameLabel.Top == addressLabel.Bottom + 8.0.Scale
        phoneLabel.Leading == nameLabel.Trailing + 8.0.Scale
        phoneLabel.CenterY == nameLabel.CenterY
        lineView.leading(12.0.Scale).trailing(12.0.Scale).height(1)
        lineView.Top == phoneLabel.Bottom + 16.0.Scale
        editButton.trailing(12.0.Scale).width(24.0.Scale).height(28.0.Scale).bottom(15.0.Scale)
        editButton.Top == lineView.Bottom + 8.0.Scale
        copyButton.trailing(60.0.Scale).width(24.0.Scale).height(28.0.Scale)
        copyButton.Top == lineView.Bottom + 8.0.Scale
        delButton.trailing(108.0.Scale).width(24.0.Scale).height(28.0.Scale)
        delButton.Top == lineView.Bottom + 8.0.Scale
        
        editButton.addTarget(self, action: #selector(clickEditButton), for: .touchUpInside)
        copyButton.addTarget(self, action: #selector(clickCopyButton), for: .touchUpInside)
        delButton.addTarget(self, action: #selector(clickDelButton), for: .touchUpInside)
    }
    
    @objc private func clickEditButton(){
        guard let md = model else { return }
        editBlock?(md)
    }
    
    @objc private func clickCopyButton(){
        guard let copyStr = model?.consignee else { return }
        UIPasteboard.general.string = copyStr
        "已复制到粘贴板".toast_success
    }
    
    @objc private func clickDelButton(){
        guard let md = model else { return }
        delBlock?(md)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
